package com.designPattern.flyweight;

/**
 * 模模拟连接对象（资源）
 */
public class Connection{
     private boolean onLine;
     private User user;

    public boolean isOnLine() {
        return onLine;
    }

    public void setOnLine(boolean onLine) {
        this.onLine = onLine;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Connection(boolean onLine, User user) {
        this.onLine = onLine;
        this.user = user;
    }
    public void connectionInfo() throws CouldNotGetConnectionException {
        if (isOnLine()) {
            System.out.println("connection-hashcode:" + this.hashCode() + ",connection-User:" + this.getUser()+"isOnline:" + isOnLine());
        }
        else {
            throw new CouldNotGetConnectionException();
        }
    }
    public void close() throws CouldNotGetConnectionException {
        if (isOnLine()) {
            setOnLine(false);
        }else {
            throw new CouldNotGetConnectionException();
        }
    }
    public void useConnection() throws CouldNotGetConnectionException {
        if (isOnLine()) {
            System.out.println(getUser() + " is using the connection:" + this.hashCode());
        }
        else {
            throw new CouldNotGetConnectionException();
        }
    }
}
